﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpHelpers.Sockets
{
    public class SocketEncrypt
    {
        private const int MASK_KEY_SIZE = 32;
        private const string MASK_KEY = "I279M&@DKdxm8oifds8203kdq~!329%D";

        /// <summary>
        /// 发送加密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static byte[] MaskData(byte[] data, int dataSize)
        {
            byte[] newData = new byte[dataSize];

            var mask = MASK_KEY.ToCharArray();

            //byte[] maskByte = new byte[MASK_KEY_SIZE];

            //for (int i = 0; i < MASK_KEY_SIZE; i++)
            //{
            //    maskByte[i] = Convert.ToByte(mask[i]);
            //}

            for (int i = 0, Index = 0; i < dataSize; ++i)
            {
                newData[i] = Convert.ToByte(data[i] ^ mask[Index]);

                if (++Index == MASK_KEY_SIZE)
                {
                    Index = 0;
                }
            }

            return newData;
        }

    }
}
